package nosql.query;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import nosql.MysqlConnector;

import org.json.simple.parser.JSONParser;

public class TestStatement {
	
	public static void insertNBAData() throws Exception{
		String url = "jdbc:mysql://localhost:3306/nosql";
		String username = "root";
		String password = "123456";
		
		MysqlConnector connector = MysqlConnector.getInstance(url, username, password);
		NosqlStatement statement = new NosqlStatement(connector);
		
		BufferedReader br = new BufferedReader(new FileReader("c:/My Dropbox/basketball_player_stats.tsv"));
		String propertyHead = br.readLine();
		String[] propertyHeadArray = propertyHead.split("\t");
		String line;
		int num = 0;
		while((line = br.readLine()) != null){
			StringBuilder builder = new StringBuilder();
			builder.append("{");
			List<String> propertyList = new ArrayList<String>();
			String[] properties = line.split("\t");
			for(int i = 0; i < properties.length; ++i){
				if(properties[i].equals(""))
					continue;
				else{
					builder.append("\"");
					builder.append(propertyHeadArray[i]);
					builder.append("\":");
					try{
						double dValue = Double.parseDouble(properties[i]);
						builder.append(dValue);
					}catch(NumberFormatException e){
						builder.append("\"");
						
						builder.append(properties[i].replace("'", "\\'").replace("\"", "\\\""));
						builder.append("\"");
					}
					builder.append(",");
				}
			}
			builder.deleteCharAt(builder.length()-1);	//	delete last ,
			builder.append("}");
			String insertJSON = builder.toString();
			num++;
			if(num % 100 == 0)
				System.out.println(num);
			statement.insert(insertJSON);
		}
	}
	
	public static void main(String[] args) throws Exception{
		/*
		 * {
		 * 	"a":1.1,
		 *  "b":[2.1, 3.2],
		 *  "c":[{"d":4},{"e":5}],
		 *  "f":{"g":11}
		 * }
		 */
//		String json = "{\"a\": 1.1, \"b\": [2.1,       3.2], \"c\": [{\"d\":4}, {\"e\":5}], \"f\":{\"g\":11}}";
//		String json = "{\"a\":{\"b\":1}, \"c\":2}";
//		String json ="{\"f\": \"str\"}";
		
		JSONParser parser = new JSONParser();
		
		String url = "jdbc:mysql://localhost:3306/nosql";
		String username = "root";
		String password = "123456";
		
		MysqlConnector connector = MysqlConnector.getInstance(url, username, password);
		NosqlStatement statement = new NosqlStatement(connector);
//		statement.insert(json);
		
		//	query
		NosqlQuery query = statement.createQuery();

		List<String> listProperty = new ArrayList<String>();
		listProperty.add("player");
		listProperty.add("average_rebounds");
//		listProperty.add("average_assists");
//		listProperty.add("average_points");
//		listProperty.add("average_steals");
//		listProperty.add("average_player_fouls");
//		listProperty.add("average_turnovers");
		
//		String queryString = "average_rebounds > 20";
////		String queryString = "average_player_fouls > 2, average_steals > 1.5, average_turnovers > 1.5";
//		List<String> list = query.query(queryString, listProperty).getJSON();
//		System.out.println(list.size() + " results.");
//		for(String str : list){
//			System.out.println(str);
//		}
		insertNBAData();
	}
}
